local gamelib = require "club.threecard.gamelib"
local require = gamelib.require
local class = require "entitas-lua.base.class"

local Systems = require "entitas-lua.entitas.Systems"

local M = class("CoroutineSystems", Systems)

function M:ctor()
    self.current = nil;
    M.super.ctor(self);
    self.executor = nil;
end

function M:execute()
    self.executor = gamelib.coroutine.start(function()
        for _, system in pairs(self._execute_systems) do
            self.current = system;
            system:_execute()
            while not system.finished do
                gamelib.coroutine.step()
            end
        end

        self.executor = nil;
        self.current = nil;
    end)
end

function M:tear_down()
    if self.executor then
        gamelib.coroutine.stop(self.executor);
        self.executor = nil;
    end

    

    self.current = nil;
    self.super.tear_down(self);
end

return M